package com.cskaoyan.wxcontroller;


import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.cart.*;
import com.cskaoyan.bean.domain.User;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 * 购物车商品表 前端控制器
 * </p>
 *
 * @author looper
 * @since 2021-11-29
 */
@Controller
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    CartService service;

    @ResponseBody
    @GetMapping("index")
    public BaseRespVo getIndex() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        CartIndexVO cartIndex = service.getIndex(userId);
        return BaseRespVo.ok(cartIndex);
    }

    @ResponseBody
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBO cartCheckedBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        CartIndexVO cartIndex = service.checked(cartCheckedBO, userId);
        return BaseRespVo.ok(cartIndex);
    }

    @ResponseBody
    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpdateBO updateBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        service.update(updateBO, userId);
        return BaseRespVo.ok(null);
    }

    @ResponseBody
    @PostMapping("add")
    public BaseRespVo add(@RequestBody AddBO addBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        Map<String, Object> resultMap = service.add(addBO, userId);
        Integer goodsCount = (Integer) resultMap.get("goodsCount");
        return BaseRespVo.ok(goodsCount);
    }

    @ResponseBody
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody DeleteBO deleteBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        CartIndexVO cartIndex = service.delete(deleteBO, userId);
        return BaseRespVo.ok(cartIndex);
    }

    @ResponseBody
    @GetMapping("goodscount")
    public BaseRespVo getGoodsCount() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            User user = (User) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = user.getId();
            Integer cartCount = service.getGoodsCount(userId);
            return BaseRespVo.ok(cartCount);
        } else {
            return BaseRespVo.ok();
        }
    }

    @ResponseBody
    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody AddBO addBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        Integer cartId = service.fastAdd(addBO, userId);
        return BaseRespVo.ok(cartId);
    }

    @ResponseBody
    @GetMapping("checkout")
    public BaseRespVo checkout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();


        CheckoutVO checkOutVO = service.checkout(userId, cartId, addressId, couponId, userCouponId, grouponRulesId);

        return BaseRespVo.ok(checkOutVO);
    }


}

